//
//  RightPreViewController.m
//  YiXiu
//
//  Created by ZHY on 16/6/27.
//  Copyright © 2016年 zzz. All rights reserved.
//

#import "RightPreViewController.h"
#import "BaseTableViewCell.h"


@interface RightPreViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSMutableArray * firstCellArr;
@property (nonatomic, copy) NSMutableArray * secondCellArr;

@property(nonatomic, strong) BaseTableViewCell * firstCell;
@property(nonatomic, strong) BaseTableViewCell * secondCell;

@property(nonatomic, strong) UIButton * editButton;

@end

@implementation RightPreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 60)];
    view.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:view];
    
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth -60, 20, 60, 40)];
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview: button];
    
   
    [self.view addSubview:self.tableView];
    
    
    
}

- (NSMutableArray *)firstCellArr{
    if (!_firstCellArr) {
        _firstCellArr = [NSMutableArray array];
        for (int i = 0; i < 20; i++) {
            
            [_firstCellArr addObject:[NSString stringWithFormat:@"%.2f",i + 1.0]];
        }
    }
    return _firstCellArr;
}

- (NSMutableArray *)secondCellArr{
    if (!_secondCellArr) {
        _secondCellArr = [NSMutableArray array];
        for (int i = 20; i < 50; i++) {
            
            [_secondCellArr addObject:[NSString stringWithFormat:@"%.2f",i +1.0]];
        }
    }
    return _secondCellArr;
}


- (UIButton *)editButton{
    if (!_editButton) {
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - 100, 2, 80, 40)];
        button.backgroundColor = HWColor(110, 205, 255);
        [button setTitle:@"编辑" forState:UIControlStateNormal];
        [button setTitle:@"完成" forState:UIControlStateSelected];
        [button addTarget:self action:@selector(ViewRightAction:) forControlEvents:UIControlEventTouchUpInside];
        _editButton = button;
    }
    return _editButton;
}


-(void)buttonAction:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}




-(UITableView *)tableView
{
    if (!_tableView) {
        UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,60, KScreenWidth, KScreenHeight - 60) style:UITableViewStyleGrouped];
        
        tableView.backgroundColor = [UIColor whiteColor];
        
        tableView.delegate = self;
        tableView.dataSource = self;
        
        _tableView = tableView;
    }
    
    return _tableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * str = @"BaseTableViewCellID";
    BaseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[BaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }
    cell.isEdit = self.editButton.selected;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        cell.array = self.firstCellArr;
        self.firstCell = cell;
        [cell setSelectBlock:^(UICollectionViewCell *item, NSIndexPath *index, BOOL isEdit) {
            
            if (isEdit) {
                [self.secondCellArr insertObject:self.firstCellArr[index.row] atIndex:0];
                [self.firstCellArr removeObjectAtIndex:index.row];
                
                [UIView animateWithDuration:1 animations:^{
                    
                    [self.tableView reloadData];
                }];
            } else{
                
                NSLog(@"%s",__func__);
            }
        }];
        
    }else if (indexPath.section == 1){
        cell.array = self.secondCellArr;
        self.secondCell = cell;
        [cell setSelectBlock:^(UICollectionViewCell *item, NSIndexPath *index, BOOL isEdit) {

            if (isEdit) {
                [self.firstCellArr addObject:self.secondCellArr[index.row]];
                [self.secondCellArr removeObjectAtIndex:index.row];
                [self.tableView reloadData];
            }else{
                
                NSLog(@"%s",__func__);
            }
            
        }];
    }
    return cell;
}




- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 30)];
    
    if (section == 0) {
        UILabel * firstHeaderLab = [[UILabel alloc]init];
        firstHeaderLab.frame =  CGRectMake(20, 0, KScreenWidth - 80, 30);
        firstHeaderLab.text = @"aaaaaaa";
        [view addSubview:firstHeaderLab];
        
        [view addSubview:self.editButton];
        
    }else{
        UILabel * secondHeaderLab = [[UILabel alloc]init];
        secondHeaderLab.frame =  CGRectMake(20, 0, KScreenWidth - 80, 30);
        secondHeaderLab.text = @"bbbbbb";
        [view addSubview:secondHeaderLab];
    }
    
    return view;
}

-(void)ViewRightAction:(UIButton *)sender
{
    sender.selected = !sender.selected;
    if (sender.selected) {
        self.firstCell.isEdit = YES;
        self.secondCell.isEdit = YES;
        [self.firstCell beginShake];
        [self.secondCell beginShake];
    }else{
        self.firstCell.isEdit = NO;
        self.secondCell.isEdit = NO;
        [self.firstCell endShake];
        [self.secondCell endShake];
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat max;
    if (indexPath.section == 0) {
        max = ceilf(self.firstCellArr.count/4.0)?ceilf(self.firstCellArr.count/4.0):1;
        
    }else if (indexPath.section == 1){
        max = ceilf(self.secondCellArr.count/4.0)>0?ceilf(self.secondCellArr.count/4.0):1;
    }
    
    CGFloat heigth = (max * itemheigth) + ((max-1) * interSpace);
    return heigth + 40;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
